<?php
namespace Index\Controller;
use Think\Controller;
class ProfileController extends Controller {
    public function index()
    {
        $m = M();
        if (!empty($_SESSION['id'])) {
            if (IS_POST) {
                $xb = $_POST['sex'];
                $sr = $_POST['nian'] . "-" . $_POST['yue'] . "-" . $_POST['ri'];
                $dz_sf = $_POST['dz_sf'];
                $dz_cs = $_POST['dz_cs'];
                $zy = $_POST['zy'];
                $js = $_POST['js'];
                $qq = $_POST['qq'];
                $dh = $_POST['dh'];
                $cymail = $_POST['cymail'];
                $wz = $_POST['wz'];
                $sql = "update user set xb = '{$xb}', sr = '{$sr}' , dz_sf = '{$dz_sf}' , dz_cs = '{$dz_cs}' , zy = '{$zy}' , js = '{$js}' , qq = '{$qq}' , dh = '{$dh}' , cymail = '{$cymail}' , wz = '{$wz}' where id = {$_SESSION['id']};";
//            var_dump($sql);
                var_dump($m->execute($sql));
//            var_dump($_POST);
            } else {
                $ary = $m->query("select * from user where id = {$_SESSION['id']}");
//            $sheng = $m->query("select * from sheng");
//            var_dump($sheng);
                $ary = $ary[0];
                $boy = explode("-", $ary['sr']);
                $this->assign('ary', $ary);
                $this->assign('boy', $boy);
                $this->display("index");
            }
        }else{
    $this->error("非法操作",U('/index'),1);
        }
    }
    public function passphrases(){
        if(IS_POST){
            $userpassword = MD5(I("post.userpassword"));
            $password = MD5(I("post.password"));
            $passwords = MD5(I("post.passwords"));
            $m = M();
            $a = $m->query("select * from user where id = {$_SESSION['id']};");
            if($a[0]['user_press'] == $userpassword){
                $sql = "update user set user_press = '{$password}' where id = {$_SESSION['id']};";
                if($password==$passwords){
                    if($m->execute($sql)){
                        session_unset();
                        session_destroy();
                        $this->error("修改密码完成,请重新登陆",U('/index/login'),1);
                    }else{
                        $this->error("修改密码失败","",1);
                    };
                }else{
                    $this->error("两次密码不一致","",1);
                }
            }else{
                $this->error("密码与原密码不一致","",1);
            }

        }else {
            $this->display("passphrases");
        }
    }
    public function xiuxiujk() {
        if (!$_FILES['upload_file']) {
            die ( 'Image data not detected!' );
        }
        if ($_FILES['upload_file']['error'] > 0) {
            switch ($_FILES ['upload_file'] ['error']) {
                case 1 :
                    $error_log = 'The file is bigger than this PHP installation allows';
                    break;
                case 2 :
                    $error_log = 'The file is bigger than this form allows';
                    break;
                case 3 :
                    $error_log = 'Only part of the file was uploaded';
                    break;
                case 4 :
                    $error_log = 'No file was uploaded';
                    break;
                default :
                    break;
            }
            die ( 'upload error:' . $error_log );
        } else {
            $img_data = $_FILES['upload_file']['tmp_name'];
            $size = getimagesize($img_data);
            $file_type = $size['mime'];
            if (!in_array($file_type, array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'))) {
                $error_log = 'only allow jpg,png,gif';
                die ( 'upload error:' . $error_log );
            }
            switch($file_type) {
                case 'image/jpg' :
                case 'image/jpeg' :
                case 'image/pjpeg' :
                    $extension = 'jpg';
                    break;
                case 'image/png' :
                    $extension = 'png';
                    break;
                case 'image/gif' :
                    $extension = 'gif';
                    break;
            }
        }
        if (!is_file($img_data)) {
            die ( 'Image upload error!' );
        }
//图片保存路径,默认保存在该代码所在目录(可根据实际需求修改保存路径)
        $id = $_SESSION['uid'];
        $save_path = './Public/newimg/'.date('Ymd',time());    //定义一个要上传头像的目录
        is_dir($save_path) || mkdir($save_path,0777,TRUE);    //如果没有这么目录,那么就创建这个目录
        $save_path = dirname( $save_path );
        $uinqid = uniqid();
        $filename = $save_path . '/' . $uinqid . '.' . $extension;
        $result = move_uploaded_file( $img_data, $filename );
        if ( ! $result || ! is_file( $filename ) ) {
            die ( 'Image upload error!' );
        }
        $newUir =  substr($filename,1);
        $m = M();
        $sql = "update user set AvatarUlr = '{$newUir}' where id = {$_SESSION['id']};";
        $m->execute($sql);
        $_SESSION['avatarulr']=$newUir;
        $a = "
        <script type=\"text/javascript\">
            window.location.href = '';
        </script>
        ";
        echo $a;
    }

}